<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/5
 * Time: 22:43
 */

namespace app\index\controller;

use app\common\controller\HomeBase;
use base\OssApi;


class Test extends HomeBase
{


    public function uploads()
    {

        if (request()->isPost()) {
            try {

                $file = request()->file('image');

                if (empty($file)) {
                    die('请选择上传的文件');
                }

                $allow_max_size   = 2 * pow(1024, 2);
                $allow_upload_ext = ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'wbmp'];
                $path             = ROOT_PATH . 'public' . DS . 'uploads';

                $info = $file->validate(['size' => $allow_max_size, 'ext' => $allow_upload_ext])->move($path);
                if (!$info) {
                    var_dump($file->getError());
                    die();
                }

                echo '<pre>';
                print_r($info);
                echo '</pre>';


                $fileName  = 'uploads/' . $info->getSaveName();
                $ossClient = OssApi::getInstance();
                $bucket    = OssApi::getBucketName();

//                $ossClient->uploadFile($bucket, $fileName, $info->getPathname());
                $content = file_get_contents($info->getPathname());
                $ossClient->putObject($bucket, $fileName,$content);
                echo '<pre>';
                print_r($ossClient);
                echo '</pre>';

            } catch (OssException $e) {
                return $e->getMessage();
            }
            print(__FUNCTION__ . ": OK" . "\n");
        } else {


            $ossClient = OssApi::getInstance();
            return $this->fetch('test/index');
        }

    }
}